package nf.translat.model;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

import jxl.Sheet;
import jxl.Workbook;

public class excel {
    Workbook wb;
    var tables=null;
    public excel(String path) {
        File directory = new File("");
        path=directory.getAbsolutePath()+"/tpl/"+path+".xls";
        File file = new File(path);
        try {
            // 创建输入流，读取Excel
            InputStream is = new FileInputStream(file.getAbsolutePath());
            // jxl提供的Workbook类
            wb = Workbook.getWorkbook(is);
            tables();
        } catch (Exception e) {
            e.printStackTrace();
        }
        
    }

    public String[] tables(){
        if(tables!=null) return tables.keys();
        tables=new var();
        int sheet_size = wb.getNumberOfSheets();
        for (int index = 0; index < sheet_size; index++) {
            Sheet sheet = wb.getSheet(index);
            tables.set(sheet.getName(),sheet);
        }

        return tables.keys();
    }

    public var read(String tabName){
        Sheet sheet=(Sheet) tables.get(tabName).source();
        var outerList=new var();
        for (int i = 0; i < sheet.getRows(); i++) {
            var innerList=new var();
            // sheet.getColumns()返回该页的总列数
            for (int j = 0; j < sheet.getColumns(); j++) {
                String cellinfo = sheet.getCell(j, i).getContents();
//                if(cellinfo.isEmpty()){
//                    continue;
//                }
                innerList.push(cellinfo);
            }
            while(innerList.size()>0 && innerList.end().toString().isEmpty()) innerList.pop();
            outerList.push(innerList);
        }

        return outerList;
    }

}
